<?php
class Account_types extends MY_Controller 
{
	var $curl = 'account_types';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model(array('/public/account_type_model', '/public/account_type_model', '/public/account_resource_model', '/public/account_permission_model'));

		$this->validation->set_fields($this->account_type_model->fields);
		
		$this->data['Title'] = T_('Account Type Manager');
		
		$this->name = T_('Account Type Manager');
		
		$this->language = parent::getLang();		
	}
		
	function index()
	{
		$this->account_type_model->search();
		
		$this->renderLayout();
	}

	function create()
	{
		$this->validation->set_fields($this->account_type_model->fields);
		
		if (count($_POST))
		{
			$rules = array('name', 'category', 'description', 'status', 'price_type', 'price_total_annual', 'price_total_monthly', 'price_tests_annual', 'price_tests_monthly', 'price_roll_over_annual', 'price_roll_over_monthly', 'carte_type',
				'carte_simple_tests_1', 'carte_simple_tests_2', 'carte_simple_tests_3', 'carte_simple_price_1', 'carte_simple_price_2', 'carte_simple_price_3',
				'carte_range_from_1', 'carte_range_from_2', 'carte_range_from_3', 'carte_range_to_1', 'carte_range_to_2', 'carte_range_to_3', 'carte_range_price_1', 'carte_range_price_2', 'carte_range_price_3',
				'reward_profit', 'reward_one_time'
				);
			
			$this->validation->set_rules($this->account_type_model->getRules($rules));
			//pr($_POST);
			if ($this->validation->run() === TRUE)
			{
				$item['name'] 					= $this->validation->name;
				$item['category'] 				= $this->validation->category;
				$item['description']			= $this->validation->description;				
				$item['status']					= $this->validation->status;
				
				$item['price_type']				= $this->validation->price_type;
				$item['price_total_annual']		= $this->validation->price_total_annual;
				$item['price_total_monthly']	= $this->validation->price_total_monthly;
				$item['price_tests_annual']		= $this->validation->price_tests_annual;
				$item['price_tests_monthly']	= $this->validation->price_tests_monthly;
				$item['price_roll_over_annual']	= $this->validation->price_roll_over_annual;
				$item['price_roll_over_monthly']= $this->validation->price_roll_over_monthly;
				
				$item['carte_type']				= $this->validation->carte_type;
				
				$item['carte_simple_tests_1']	= $this->validation->carte_simple_tests_1;
				$item['carte_simple_tests_2']	= $this->validation->carte_simple_tests_2;
				$item['carte_simple_tests_3']	= $this->validation->carte_simple_tests_3;
				$item['carte_simple_price_1']	= $this->validation->carte_simple_price_1;
				$item['carte_simple_price_2']	= $this->validation->carte_simple_price_2;
				$item['carte_simple_price_3']	= $this->validation->carte_simple_price_3;
				
				$item['carte_range_from_1']		= $this->validation->carte_range_from_1;
				$item['carte_range_from_2']		= $this->validation->carte_range_from_2;
				$item['carte_range_from_3']		= $this->validation->carte_range_from_3;
				$item['carte_range_to_1']		= $this->validation->carte_range_to_1;
				$item['carte_range_to_2']		= $this->validation->carte_range_to_2;
				$item['carte_range_to_3']		= $this->validation->carte_range_to_3;
				$item['carte_range_price_1']	= $this->validation->carte_range_price_1;
				$item['carte_range_price_2']	= $this->validation->carte_range_price_2;
				$item['carte_range_price_3']	= $this->validation->carte_range_price_3;			
				
				$item['reward_profit']			= $this->validation->reward_profit;
				$item['reward_one_time']		= $this->validation->reward_one_time;
				
				$this->account_type_model->insert($item);

				redirect($this->curl);
			}
		}
		else
		{
			$this->validation->price_type = 1;
			$this->validation->carte_type = 1;
		}
		
		$this->renderLayout();			
	}

	function edit($id = null)
	{
		$this->validation->set_fields($this->account_type_model->fields);
		
		if (!empty($id))
		{
			$object = $this->account_type_model->get($id);

			if (!count($object))
			{
				$this->renderElement('query_no_object');
				return false; 
			}
			
			if (!count($_POST))
			{
				$this->validation->id 	 		= $object['id'];
				$this->validation->name  		= $object['name'];
				$this->validation->category  	= $object['category'];
				$this->validation->description  = $object['description']; 
				$this->validation->status 		= $object['status'];

				$this->validation->price_type 				= $object['price_type'];
				$this->validation->price_total_annual 		= $object['price_total_annual'];
				$this->validation->price_total_monthly 		= $object['price_total_monthly'];
				$this->validation->price_tests_annual 		= $object['price_tests_annual'];
				$this->validation->price_tests_monthly 		= $object['price_tests_monthly'];
				$this->validation->price_roll_over_annual 	= $object['price_roll_over_annual'];
				$this->validation->price_roll_over_monthly 	= $object['price_roll_over_monthly'];
				
				$this->validation->carte_type 	= $object['carte_type'];
				
				$this->validation->carte_simple_tests_1 = $object['carte_simple_tests_1'];
				$this->validation->carte_simple_tests_2 = $object['carte_simple_tests_2'];
				$this->validation->carte_simple_tests_3 = $object['carte_simple_tests_3'];
				$this->validation->carte_simple_price_1 = $object['carte_simple_price_1'];
				$this->validation->carte_simple_price_2 = $object['carte_simple_price_2'];
				$this->validation->carte_simple_price_3 = $object['carte_simple_price_3'];
				
				$this->validation->carte_range_from_1 	= $object['carte_range_from_1'];
				$this->validation->carte_range_from_2 	= $object['carte_range_from_2'];
				$this->validation->carte_range_from_3 	= $object['carte_range_from_3'];
				$this->validation->carte_range_to_1 	= $object['carte_range_to_1'];
				$this->validation->carte_range_to_2 	= $object['carte_range_to_2'];
				$this->validation->carte_range_to_3 	= $object['carte_range_to_3'];
				$this->validation->carte_range_price_1 	= $object['carte_range_price_1'];
				$this->validation->carte_range_price_2 	= $object['carte_range_price_2'];
				$this->validation->carte_range_price_3 	= $object['carte_range_price_3'];
				
				$this->validation->reward_profit 		= $object['reward_profit'];
				$this->validation->reward_one_time 		= $object['reward_one_time'];
			}
			else
			{
				$rules = array('name', 'category', 'description', 'status', 'price_type', 'price_total_annual', 'price_total_monthly', 'price_tests_annual', 'price_tests_monthly', 'price_roll_over_annual', 'price_roll_over_monthly', 'carte_type',
					'carte_simple_tests_1', 'carte_simple_tests_2', 'carte_simple_tests_3', 'carte_simple_price_1', 'carte_simple_price_2', 'carte_simple_price_3',
					'carte_range_from_1', 'carte_range_from_2', 'carte_range_from_3', 'carte_range_to_1', 'carte_range_to_2', 'carte_range_to_3', 'carte_range_price_1', 'carte_range_price_2', 'carte_range_price_3',
					'reward_profit', 'reward_one_time'
				);
				
				$this->validation->set_rules($this->account_type_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 		= $this->validation->id;
					$item['name'] 		= $this->validation->name;
					$item['category'] 	= $this->validation->category;
					$item['description']= $this->validation->description;
					$item['status'] 	= $this->validation->status;
					
					$item['price_type']				= $this->validation->price_type;
					$item['price_total_annual']		= $this->validation->price_total_annual;
					$item['price_total_monthly']	= $this->validation->price_total_monthly;
					$item['price_tests_annual']		= $this->validation->price_tests_annual;
					$item['price_tests_monthly']	= $this->validation->price_tests_monthly;
					$item['price_roll_over_annual']	= $this->validation->price_roll_over_annual;
					$item['price_roll_over_monthly']= $this->validation->price_roll_over_monthly;
					
					$item['carte_type']				= $this->validation->carte_type;
					
					$item['carte_simple_tests_1']	= $this->validation->carte_simple_tests_1;
					$item['carte_simple_tests_2']	= $this->validation->carte_simple_tests_2;
					$item['carte_simple_tests_3']	= $this->validation->carte_simple_tests_3;
					$item['carte_simple_price_1']	= $this->validation->carte_simple_price_1;
					$item['carte_simple_price_2']	= $this->validation->carte_simple_price_2;
					$item['carte_simple_price_3']	= $this->validation->carte_simple_price_3;
					
					$item['carte_range_from_1']		= $this->validation->carte_range_from_1;
					$item['carte_range_from_2']		= $this->validation->carte_range_from_2;
					$item['carte_range_from_3']		= $this->validation->carte_range_from_3;
					$item['carte_range_to_1']		= $this->validation->carte_range_to_1;
					$item['carte_range_to_2']		= $this->validation->carte_range_to_2;
					$item['carte_range_to_3']		= $this->validation->carte_range_to_3;
					$item['carte_range_price_1']	= $this->validation->carte_range_price_1;
					$item['carte_range_price_2']	= $this->validation->carte_range_price_2;
					$item['carte_range_price_3']	= $this->validation->carte_range_price_3;
					
					$item['reward_profit']			= $this->validation->reward_profit;
					$item['reward_one_time']		= $this->validation->reward_one_time;
				
					$this->account_type_model->update($item);
					
					redirect($this->curl);
				}
			}
		}
		
		$this->renderLayout();
	}
		
	function deleteItems()
	{
		echo $data = $this->account_type_model->delete($_REQUEST['items']);		
	}

	function delete($id)
	{
		$data = $this->account_type_model->delete($id);

		redirect($this->curl . '/index');
	}
	
	function delete_role($id)
	{
		$data = $this->account_type_model->delete($id);

		redirect($this->curl .'/roles');
	}
	
	function resources()
	{
		$this->account_resource_model->search();

		$this->data['Title'] .= ' / '. T_('Resources of Website'); 
				
		$this->renderLayout();
	}

	function create_resource()
	{
		$this->validation->set_fields($this->account_resource_model->fields);
		
		if (count($_POST))
		{
			$rules = array('category', 'controller', 'action', 'description');
			$this->validation->set_rules($this->account_resource_model->getRules($rules));
						
			if ($this->validation->run() === TRUE)
			{
				$item['category']	 = $this->validation->category;
				$item['controller']	 = $this->validation->controller;
				$item['action']		 = $this->validation->action;
				$item['description'] = $this->validation->description;
				
				$this->account_resource_model->insert($item);

				redirect($this->curl .'/resources');
			}
		}

		$this->submenu = "_menu_resources";
		
		$this->renderLayout();			
	}
		
	function edit_resource($id = null)
	{
		$this->validation->set_fields($this->account_resource_model->fields);
		
		if (!empty($id))
		{
			$object = $this->account_resource_model->get($id);
			
			if (!count($object))
			{
				$this->renderElement('query_no_object');
				return false; 
			}
			
			if (!count($_POST))
			{
				$this->validation->id 	 		= $object['id'];
				$this->validation->category 	= $object['category']; 
				$this->validation->controller 	= $object['controller'];
				$this->validation->action  		= $object['action']; 
				$this->validation->description  = $object['description']; 			 
			}
			else
			{
				$rules = array('category', 'controller', 'action', 'description');
				$this->validation->set_rules($this->account_resource_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id'] 		 = $this->validation->id;
					$item['category']	 = $this->validation->category;
					$item['controller']	 = $this->validation->controller;
					$item['action']		 = $this->validation->action;
					$item['description'] = $this->validation->description;					
					
					$this->account_resource_model->update($item);
					
					redirect($this->curl .'/resources');
				}
			}
		}
	
		$this->submenu = "_menu_resources";
		
		$this->renderLayout();
	}

	function delete_resources()
	{
		$data = $this->account_resource_model->delete($_REQUEST['items']);

		echo $this->utils->php2js($data);
	}	
	
	function permissions($id_role = null)
	{
		if (count($_POST))
		{
			$sql = 'DELETE FROM account_permissions WHERE id_account_type=' . $this->db->escape($id_role);
			$this->db->query($sql);

			if (!empty($_POST['rid']))
            {				
				foreach($_POST['rid'] as $rid)
				{
					$x = explode('_', $rid);
					$item['id_account_resources'] 	= $x[0];
					$item['id_account_type'] 		= $x[1];
					$this->account_permission_model->insert($item);		
    			}
            }			
			$this->log->writeMessage('done', T_('Permissions was saved.'));

			redirect($this->curl . '/permissions/'. $id_role);		
		}
		
		$this->account_permission_model->search();
		
		$this->data['roles'] = $this->account_type_model->getSelectOptions($id_role);		
		$this->data['resources'] = $this->account_resource_model->getAll();
		$this->data['permissions'] = $this->account_permission_model->get();
		$this->data['id_role'] = $id_role;
		$this->data['Title'] .= ' / '. T_('Permissions'); 
				
		$this->renderLayout();
	}
}
?>